// pages/diary/list/list.js
import {
  OrderModel
} from '../../../model/order'
var order = new OrderModel()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    id: 0,
    name: '',
    code: '',
    detailList: [],
    total: 0

  },
  actionDetail(e) {
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      urls: this.data.detailList[index].imgs,
      current: e.currentTarget.dataset.url
    })
  },
  toAlbum() {
    wx.navigateTo({
      url: '/pages/album/album?id=' + this.data.id + "&name=" + this.data.name + "&code=" + this.data.code,
    })
  },
  toDetail(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/diary/detail/detail?id=' + id,
    })
  },
  getList() {
    order.customMadeDiaryList('', '', this.data.id).then(res => {
      console.log(res)
      if (res.code == 0) {
        res.data.records.forEach(element => {
          element.imgs = element.imgs.split(';;')
          if (element.explains.length > 100) {
            element.explains = element.explains.substring(0, 100) + "..."
          }
        });
        console.log(res.data.records)
        this.setData({
          detailList: res.data.records,
          total: res.data.total
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  save() {
    var imgSrc = "http://yijiao.oss-cn-qingdao.aliyuncs.com/images/http://tmp/wx1b4e5e756cd48af1.o6zAJsws4grEQvYrWTjBigy-6QaU.0llhudiKSF2V955a1c48350d9328ef064b4d36d12746.jpg"
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权")
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                  } else {
                    console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          },
          complete(res) {
            console.log(res);
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id,
      name: options.name,
      code: options.code
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})